package manager.dao.proxy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import manager.dao.impl.FriendDAOImpl;
import manager.database.DatabaseSqlSession;
import manager.vo.Friend;

public class FriendDAOProxy {
	private FriendDAOImpl dao = null;
	private FriendDAOProxy() throws Exception{
		this.dao = new FriendDAOImpl(DatabaseSqlSession.getSqlSession());
	}
	public static FriendDAOProxy getInstance() throws Exception{
		return new FriendDAOProxy();
	}
	//查询朋友
   public List<Friend> selectFriend(Map<String,Object>map){
	   List<Friend> list = null;
	  try {
		 list = this.dao.selectFriend(map);
	  } catch (Exception e) {
		e.printStackTrace();
	  }
	  finally{
  		DatabaseSqlSession.closeSqlSession();
  	  }
	  return list;
   }
   //添加
   public boolean addFriend(Friend friend){
	 boolean flag = false;
	 try {
		if(this.dao.addFriend(friend)>0){
			flag = true;
			DatabaseSqlSession.commit();
		}
	 } catch (Exception e) {
		e.printStackTrace();
	 }
	 finally{
 		DatabaseSqlSession.closeSqlSession();
 	 }
	 return flag;
   }
    public int selectCount(Integer userid){
	   int count = 0;
	   try {
		count = this.dao.selectCount(userid);
	  } catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	  }finally{
		DatabaseSqlSession.closeSqlSession();
	  }
	   return count;
    }
    //删除
    public boolean deleteFriend(Integer userid,Integer id){
    	boolean flag = false;
    	try {
			if(this.dao.deleteFriend(userid, id)>0){
				flag = true;
				DatabaseSqlSession.commit();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally{
    		DatabaseSqlSession.closeSqlSession();
    	}
    	return flag;
    }
    //更新
    public boolean updateFriend(Map<String,Object>map) {
    	boolean flag = true;
    	try {
			if(this.dao.updateFriend(map)>0){
				flag = true;
			    DatabaseSqlSession.commit();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally{
    		DatabaseSqlSession.closeSqlSession();
    	}
    	return flag;
    }
//   public static void main(String[] args) {
//	   //Friend friend = new Friend(0,"李伍各","男",28,new java.sql.Date(new java.util.Date().getTime()),"15270897054","无敌",4);
//	   Map<String,Object> map = new HashMap<String,Object>();
//	   map.put("userid", 4);
//	   try {
//		//System.out.println(FriendDAOProxy.getInstance().addFriend(friend));
//		   List<Friend> list = FriendDAOProxy.getInstance().selectFriend(map);
//		   for (Friend friend : list) {
//			   System.out.println(friend.toString());  
//		   }
//	   } catch (Exception e) {
//		 // TODO Auto-generated catch block
//		 e.printStackTrace();
//	   }
//   }
}
